package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.cache.CacheData;
import pers.cj.pdos.cache.CacheRemove;
import pers.cj.pdos.cache.CacheRemovePermission;
import pers.cj.pdos.cache.CacheUtils;
import pers.cj.pdos.constant.CacheConstant;
import pers.cj.pdos.entity.RolePermission;
import pers.cj.pdos.entity.UserRole;
import pers.cj.pdos.mapper.RolePermissionMapper;
import pers.cj.pdos.mapper.UserRoleMapper;
import pers.cj.pdos.service.IRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {
    @Autowired
    RolePermissionMapper rolePermissionMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    @CacheData(clazz = RolePermissionServiceImpl.class, param = "#roleId")
    public List<RolePermission> listByRole(String roleId, String companyId) {
        return rolePermissionMapper.selectList(new QueryWrapper<RolePermission>().eq("role_id",roleId).eq("company_id",companyId));
    }

    @Override
    @CacheRemovePermission(roleId = "#roleId")
    @CacheRemove(clazz = RolePermissionServiceImpl.class,methodName = "listByRole", param = "#roleId")
    public void updateList(List<RolePermission> list, String roleId, String companyId) {
        long time = System.currentTimeMillis();
        System.out.println("---------------------updateList--------------------------");
        //删除角色的权限
        rolePermissionMapper.delete(new QueryWrapper<RolePermission>().eq("role_id",roleId).eq("company_id",companyId));

        System.out.println("---------------------updateList["+(System.currentTimeMillis()-time)+"]--------------------------");
        time = System.currentTimeMillis();
        //新增角色的权限
        if (!Utils.isEmpty(list)){
            rolePermissionMapper.insertBatch(list);
        }
        System.out.println("---------------------updateList["+(System.currentTimeMillis()-time)+"]--------------------------");
    }
}
